package com.lzb.permanentcalendar.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzb.permanentcalendar.BaseNoModelFragment;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.databinding.FragmentDateSwapBinding;
import com.lzb.permanentcalendar.dialog.LunarDateDialog;
import com.lzb.permanentcalendar.dialog.SolarDateDialog;
import com.lzb.permanentcalendar.dialog.SolarTimeDialog;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DateSwapFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DateSwapFragment extends BaseNoModelFragment<FragmentDateSwapBinding> implements View.OnClickListener {

    private SolarDateDialog solarDateDialog;
    private LunarDateDialog lunarDateDialog;

    @Override
    protected int onCreate() {
        return R.layout.fragment_date_swap;
    }

    @Override
    protected void initView() {
        mDataBinding.llSolar.setOnClickListener(this);
        mDataBinding.llLunar.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llSolar) {//阳历
            showSolarDateDialog();
        } else if (id == R.id.llLunar) {//阴历
            showLunarDateDialog();
        }
    }

    @Override
    protected void initData() {
        Solar solar = Solar.fromDate(new Date());
        Lunar lunar = solar.getLunar();
        mDataBinding.tvSolar.setText(solar.getYear() + "-" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "-" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay()));
        mDataBinding.tvLunar.setText(lunar.getYear() + "年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese());
    }

    private void showSolarDateDialog() {
        if (solarDateDialog == null) solarDateDialog = new SolarDateDialog(mContext);
        solarDateDialog.setListener(new SolarDateDialog.Ilistener() {
            @Override
            public void clickConfirm(int year, int mo, int days) {
                Solar solar = Solar.fromYmd(year, mo, days);
                Lunar lunar = solar.getLunar();
                mDataBinding.tvSolar.setText(solar.getYear() + "-" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "-" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay()));
                mDataBinding.tvLunar.setText(lunar.getYear() + "年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese());
            }
        });
        solarDateDialog.show();
    }

    private void showLunarDateDialog() {
        if (lunarDateDialog == null) lunarDateDialog = new LunarDateDialog(mContext);
        lunarDateDialog.setListener(new LunarDateDialog.Ilistener() {
            @Override
            public void clickConfirm(int year, int mo, int days) {
                Lunar lunar = new Lunar(year, mo, days);
                // 转换为阳历
                Solar solar = lunar.getSolar();
                mDataBinding.tvSolar.setText(solar.getYear() + "-" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "-" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay()));
                mDataBinding.tvLunar.setText(lunar.getYear() + "年" + lunar.getMonthInChinese() + "月" + lunar.getDayInChinese());
            }
        });
        lunarDateDialog.show();
    }
}